package net.zjitc.servlet.goods;

import net.zjitc.domain.Goods;
import net.zjitc.service.GoodsService;
import net.zjitc.service.impl.GoodsServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * //1、查询：数据库(略)--->作业 增删除改查，通过id查询
 * //2、request、session、servletContext (总结他们的区别与联系)手写上交，周四上交
 */

@WebServlet(name = "GoodsListServlet", urlPatterns = "/goodsList")
public class GoodsListServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询：数据库(略)--->作业
        //为了演示，直接new 对象
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();

        //session.setMaxInactiveInterval(120);

        GoodsService goodsService = new GoodsServiceImpl();
        List<Goods> list = goodsService.findALL();

        //response.getWriter().write(list); //json
        //将数据设置到request域中
        request.setAttribute("li", list);
        //请求转发，共享一个request
        RequestDispatcher dispatcher = request.getRequestDispatcher("/goods/list.jsp");
        dispatcher.forward(request, response);


    }
}
